import urllib.request
import urllib.parse

content = input('请输入需要翻译的内容: ')

# translate_o   吧_o去掉可以爬虫 url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'


# 下面这些都先按照我们之前抓包获取到的数据
data = {}
data['i'] = content
data['from'] = "AUTO"
data['to'] = "AUTO"
data['smartresult'] = "dict"
data['client'] = "fanyideskweb"
data['salt'] = "15647463093426"
data['sign'] = "47521708ac619a162f57370325807e31"
data['ts'] = "1564746309342"
data['bv'] = "ce1fe729a12a6b5169046dd3aac11e4c"
data['doctype'] = "json"
data['version'] = "2.1"
data['keyfrom'] = "fanyi.web"
data['action'] = "FY_BY_CLICKBUTTION"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3969.0 Safari/537.36"
}

# 对数据进行编码处理
formdata = urllib.parse.urlencode(data).encode('utf-8')
 
req=urllib.request.Request(url, data=formdata, headers=headers)  #模拟post请求  如果data有值就是post 没有就是get

print(urllib.request.urlopen(req).read().decode('utf-8'))


